/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package station;

/**
 *
 * @author safe
 */
public final class DataEmpfangeneNachrichten {

//    private static Map<Long, byte[]> empfangeneNachrichtenMitEmpfangszeit = new ConcurrentHashMap<Long, byte[]>();
    private static String lastMessageClass = "";
    private static int lastMessageSlot = -1;
    private static long lastMessageSendTime = 0;
    private static long lastMessageReceiveTime = 0;
    private static long lastCollisionTimestamp = 0;
    public static final int diffForDetectCollision = 15;
    private static boolean sendingCollision = false;

    private DataEmpfangeneNachrichten() {
    }

    public static void setSendingCollision(boolean bool) {
        sendingCollision = bool;
    }

    public static boolean getSendingCollision() {
        return sendingCollision;
    }

    public static String getLastMessageClass() {
        return lastMessageClass;
    }

    public static int getLastMessageSlot() {
        return lastMessageSlot;
    }

    synchronized static boolean collisionDetect(long actualMessageReceiveTime) {
        // wir brauchen auch diesen vergleich, weil lastMessageReceiveTime eventuell 0 ist nach collisionCleanUp
        if (getSlotNumber(lastCollisionTimestamp) == getSlotNumber(actualMessageReceiveTime)) {
            return true;
        } else if (getSlotNumber(lastMessageReceiveTime) == getSlotNumber(actualMessageReceiveTime)) {
            return true;
        } else {
            return false;
        }
    }

    static int getSlotNumber(long timestamp) {
        return (int) (timestamp % 1000) / 40;
    }

    synchronized static void collisionCleanUp(long actualMessageReceiveTime) {
        lastCollisionTimestamp = actualMessageReceiveTime;
        lastMessageClass = "";
        lastMessageSlot = -1;
        lastMessageSendTime = 0;
        lastMessageReceiveTime = 0;
    }

    static synchronized long[] setLastMessageAsValid(String stationClass, int slotNumber, long sendTimeStamp, long receiveTimestamp) {
        long[] times = new long[2];
        if (lastMessageClass.equals("A")) {
            times[0] = lastMessageSendTime;
            times[1] = lastMessageReceiveTime;
        }
        lastMessageClass = stationClass;
        lastMessageSlot = slotNumber;
        lastMessageSendTime = sendTimeStamp;
        lastMessageReceiveTime = receiveTimestamp;
        return times;
    }
}
